#coding:utf-8
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import Group, BaseUserManager, AbstractBaseUser
from lovewith.website.models import Province, City


class MtUserManager(BaseUserManager):
    def create_user(self, password=None, app_uid=None, username=None, email=None, gender=None, avatar=None,
                    account_type=None):
        if password and username and email and avatar and account_type:
            user = self.model(app_uid=app_uid, username=username, email=email, gender=gender, avatar=avatar,
                              account_type=account_type)
            user.set_password(password)
            user.save(using=self._db)
            return user
        else:
            raise ValueError(u'数据不完整')

    def create_superuser(self, app_uid, username, password):
        user = self.create_user(app_uid=app_uid, username=username, account_type=1, password=password)
        user.is_admin = True
        user.save(using=self._db)
        return user


class MtUser(AbstractBaseUser):
    account_type = (('1', u'新浪微博'), ('2', u'QQ空间'),)
    gender_type = (('m', u'男'), ('f', u'女'),)

    app_uid = models.CharField(max_length=32, blank=True, null=True)
    username = models.CharField(max_length=100, unique=True, verbose_name=u'用户名')
    full_name = models.CharField(blank=True, null=True, max_length=100, verbose_name=u'姓名')
    email = models.EmailField(blank=True, null=True, verbose_name=u'邮箱')
    gender = models.CharField(choices=gender_type, max_length=1, default='m', verbose_name=u'性别')
    avatar = models.CharField(max_length=256, verbose_name=u'头像')
    phone = models.CharField(blank=True, null=True, max_length=13, verbose_name=u'电话')
    qq = models.CharField(blank=True, null=True, max_length=13, verbose_name=u'QQ')
    #商家||普通用户
    group = models.ForeignKey(Group, default=1, verbose_name=u'用户组')
    #新浪微博||腾讯微博
    account_type = models.CharField(choices=account_type, max_length=1, default=1, verbose_name=u'用户类型')
    access_token = models.CharField(blank=True, max_length=32, verbose_name=u'授权Token')
    token_expires = models.CharField(blank=True, max_length=32, verbose_name=u'Token过期时间')
    join_date = models.DateTimeField(default=timezone.now, verbose_name=u'加入时间')
    sync_option = models.TextField(blank=True, default='{"p": "0", "c": "0", "l": "0", "f": "0"}',
                                   verbose_name=u'内容同步设置')
    #省
    province = models.ForeignKey(Province, blank=True, null=True, verbose_name=u'省')
    #市
    city = models.ForeignKey(City, blank=True, null=True, verbose_name=u'市')
    is_active = models.BooleanField(default=True, verbose_name=u'激活帐号')
    is_admin = models.BooleanField(default=False, verbose_name=u'管理员')

    objects = MtUserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['uid']


    def __unicode__(self):
        return self.username


    def get_full_name(self):
        # The user is identified by their email address
        return self.username


    def get_short_name(self):
        # The user is identified by their email address
        return self.username


    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True


    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True


    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin


    class Meta:
        verbose_name_plural = u'用户管理'
        db_table = 'mt_user'


#授权网站列表
class MtUserAuth(models.Model):
    user = models.ForeignKey(MtUser, verbose_name=u'用户')
    username = models.CharField(max_length=32, blank=True, verbose_name=u'授权网站用户名')
    type = models.SmallIntegerField(verbose_name=u'帐号类型')
    access_token = models.CharField(max_length=32, verbose_name=u'授权Token')
    token_expires = models.CharField(max_length=32, verbose_name=u'Token过期时间')
    sync_config = models.TextField(default='{"post": "1", "comment": "1", "like": "1", "album": "1"}',
                                   verbose_name=u'内容同步设置')

    def __unicode__(self):
        return self.access_token

    class Meta:
        verbose_name_plural = u'授权网站'
        db_table = 'mt_user_auth'


#婚礼信息
class MtUserWeddingData(models.Model):
    bride = models.CharField(max_length=30, blank=True, null=True, verbose_name=u'新娘')
    bridegroom = models.CharField(max_length=30, blank=True, null=True, verbose_name=u'新郎')
    wedding_date = models.CharField(max_length=30, blank=True, null=True, verbose_name=u'日期')
    wedding_time = models.CharField(max_length=30, blank=True, null=True, verbose_name=u'时间')
    city = models.CharField(max_length=32, blank=True, null=True, verbose_name=u'城市')
    address = models.CharField(max_length=255, blank=True, null=True, verbose_name=u'地点')
    #新郎：1 新娘: 2
    user_role = models.SmallIntegerField(max_length=1, default=1, verbose_name=u'用户身份')
    user = models.ForeignKey(MtUser, verbose_name=u'创建者')

    def __unicode__(self):
        return self.bride

    class Meta:
        verbose_name_plural = u'婚礼信息'
        db_table = 'mt_user_wedding_data'


#用户通知
class MtUserMessage(models.Model):
    #通知类型：system|other
    type = models.CharField(max_length=10, verbose_name=u'通知类型')
    title = models.CharField(max_length=255, verbose_name=u'通知标题')
    content = models.TextField(verbose_name=u'通知内容')
    receiver = models.ForeignKey(MtUser, verbose_name=u'接收者')
    has_read = models.BooleanField(default=0, verbose_name=u'是否已读')
    notice_date = models.DateTimeField(default=timezone.now, verbose_name=u'通知发送时间')

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name_plural = u'用户通知'
        db_table = 'mt_user_message'


#找回密码
class MtuserGetPassword(models.Model):
    confirm_key = models.CharField(max_length=255, verbose_name=u'校验key')
    expires_date = models.DateTimeField(default=timezone.now, verbose_name=u'过期时间')
    used = models.SmallIntegerField(default=0, verbose_name=u'是否已被使用')
    user = models.ForeignKey(MtUser, verbose_name=u'用户')

    def __unicode__(self):
        return self.confirm_key

    class Meta:
        verbose_name_plural = u'找回密码'
        db_table = 'mt_user_getpassword'
